<?php

namespace App\Http\Controllers\Console;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use itbdw\QiniuStorage\QiniuStorage;


class QiniuController extends Controller
{
    public function uploads(Request $request){

        $disk = \Storage::disk('qiniu');
        $time = date('Y/m/d');
        $file = $request->file('pic');
        $filePath =[];  // 定义空数组用来存放图片路径
        foreach ($file as $key => $value) {
            // 判断图片上传中是否出错
            if (!$value->isValid()) {
                exit("上传图片出错，请重试！");
            }
            if(!empty($value)){//此处防止没有多文件上传的情况
                $allowed_extensions = ["png", "jpg", "gif"];
                if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
                    exit('您只能上传PNG、JPG或GIF格式的图片！');
                }
                $filename = $disk->put($time, $value);


                $pic = $disk->getDriver()->downloadUrl($filename);
                $filePath[] = $pic;

            }
        }
        //多张图片上传完毕
        return json_encode(['code'=>200,'msg'=>'图片上传至七牛云成功','data'=>$filePath]);

    }

}
